﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESHOP.Domain;
namespace ESHOP.DAO
{
    public class TheyeucauDAO:BaseDAO
    {
        public THEYEUCAU Get(String ma)
        {
            return _db.THEYEUCAUs.Where(p => p.ID.Equals(ma)&&p.ISSHOW.Equals(true)).SingleOrDefault();
        }
        public List<THEYEUCAU> GetListForCompany_Selling(String ma)
        {
            return _db.THEYEUCAUs.Where(p => p.MACT.Equals(ma)&&p.ISSHOW.Equals(true)&&p.LOAI==1).ToList();
        }
        public List<THEYEUCAU> GetListForCompany_Buying(String ma)
        {
            return _db.THEYEUCAUs.Where(p => p.MACT.Equals(ma) && p.ISSHOW.Equals(true) && p.LOAI == 2).ToList();
        }
       
        public int Delete(String ma)
        {
            try
            {
                var objdb = Get(ma);
                if (objdb != null)
                {
                    objdb.ISSHOW = false;
                    _db.SubmitChanges();
                    return 1;
                }
            }
            catch
            {
                return 0;
            }
            return 0;
        }

        public int Insert(THEYEUCAU obj)
        {
            try
            {
                _db.THEYEUCAUs.InsertOnSubmit(obj);
                _db.SubmitChanges();
                return 1;
            }
            catch
            {
                return 0;
            }
        }

        public string NewId()
        {
            string sReturn = "";

            string sToday = "";
            sToday = DateTime.Now.Year.ToString();
            sToday = DateTime.Now.Month < 10
                         ? sToday + "0" + DateTime.Now.Month.ToString()
                         : sToday + DateTime.Now.Month.ToString();

            sToday = DateTime.Now.Day < 10
                         ? sToday + "0" + DateTime.Now.Day.ToString()
                         : sToday + DateTime.Now.Day.ToString();

            sReturn = (from p in _db.THEYEUCAUs.Where(p => p.ID.Substring(0, 8).Contains(sToday))
                       select p.ID).Max();

            if (!string.IsNullOrEmpty(sReturn))
            {
                int thutu = int.Parse(sReturn.Substring(8, 3));
                thutu = thutu + 1;
                if (thutu < 10)
                {
                    sReturn = sToday + "00" + thutu.ToString();
                }
                else if (thutu < 100)
                {
                    sReturn = sToday + "0" + thutu.ToString();
                }
                else if (thutu < 1000)
                {
                    sReturn = sToday + thutu.ToString();
                }
            }
            else
            {
                sReturn = sToday + "001";
            }

            return sReturn;
        }
    }
}
